package com.example.myhotel.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.myhotel.common.R;
import com.example.myhotel.entity.Employees;
import com.example.myhotel.service.EmployeesService;


import com.example.myhotel.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("employee")
@CrossOrigin

@Slf4j
public class EmployeesController {
    @Autowired
    private EmployeesService employeesService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        Page<Employees> empPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Employees> wrapper = new LambdaQueryWrapper<>();
        employeesService.page(empPage,wrapper);
        return  R.success(empPage);
    }
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody Employees employees, HttpServletRequest request){
        String phone = employees.getPhone();
        if (phone != null){
            LambdaQueryWrapper<Employees> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Employees::getPhone, phone);
            Employees one = employeesService.getOne(wrapper);
            if (one == null){
                return R.error("该手机号不是内部员工的");
            }
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
//            SMSUtils.sendMessage("品优购","SMS_142946316",phone,code);
            log.info("登录的验证码是：{}",code);
            request.getSession().setAttribute(phone,code);
            return R.success("成功发送验证码");
        }
        return R.error("短信发送失败");
    }
    @PostMapping("login")
    public R<Map> Login(@RequestBody Employees employees,HttpServletRequest request){
        LambdaQueryWrapper<Employees> wrapper = new LambdaQueryWrapper<>();

        String phone = employees.getPhone();
        //获取前端传输来的验证码
        String code = employees.getCode();
        //获取生成的验证码
        Object codeSession = request.getSession().getAttribute(phone);
        log.info("获取的验证吗---------:{}",codeSession);
        if (codeSession != null && code.equals(codeSession)){
            wrapper.eq(Employees::getAdmin,1)
                    .eq(Employees::getName,employees.getName())
                    .eq(Employees::getPhone,employees.getPhone());
            Employees one = employeesService.getOne(wrapper);
            if (one == null){
                return R.error("该用户没有权限登录");
            }
            //在session写入数据adminId
            request.getSession().setAttribute("adminId",one.getId());
            String token = UUID.randomUUID().toString();
            //写数据进数据库redis 并且三十分钟超时
            stringRedisTemplate.opsForValue().set("adminId",one.getId(),30,TimeUnit.HOURS);
            stringRedisTemplate.opsForValue().set("aToken",token,1,TimeUnit.HOURS);

            //传token到后端
            Map<String,Object> map = new HashMap<>();
            map.put("aToken",token);
            map.put("adminId",one.getId());
            return R.success(map);

        }
        return R.error("验证码错误");
    }

    @PostMapping("/add")
    public R<String> add(@RequestBody Employees employees){
        log.info("employee--add-{}",employees);
        if (employees !=null){
            LambdaQueryWrapper<Employees> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Employees::getPhone,employees.getPhone());
            Employees one = employeesService.getOne(wrapper);
            if (one != null){
                R.error("该员工已存在");
            }
            employeesService.save(employees);
            return R.success("成功添加");
        }
        return R.error("添加失败");
    }
    @PutMapping
    public R<String> update(@RequestBody Employees employees){
//        log.info("employee--update-{}",employees);
        if (employees !=null){
            if (employees.getExist() != null && employees.getExist().equals("离职")){
                employees.setEndTime(LocalDateTime.now());
            }
            employeesService.updateById(employees);
            return R.success("更新成功");
        }
        return R.error("更新失败");
    }
    @DeleteMapping
    public R<String> delete(String id){
        log.info("iddelete------{}",id);
        Employees employees = employeesService.getById(id);
        if (employees.getEndTime() == null){
            return R.error("删除失败！该员工未离职");
        }
        employeesService.removeById(id);
        return R.success("删除成功");
    }

}
